
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>IOS NSString常用Category - 许龙武的技术博客</title>
  <meta name="author" content="许龙武">

  
  <meta name="description" content="我们经常会在项目中用一些NSString的Category，可以记录下来，免得每次都要查。比如NSString的URL编码、MD5加密、自适应大小等等。 方法解析 汉字转拼音 1
2
3
4
5
6
-(NSString *)transformToPinyin { NSMutableString &hellip;">
  

  <!-- http://t.co/dKP3o1e -->
  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  
  <link rel="canonical" href="http://loongwoo.github.io/blog/2015/07/08/at-ios-nsstringchang-yong-category/">
  <link href="/favicon.png" rel="icon">
  <link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css">
  <link href="/atom.xml" rel="alternate" title="许龙武的技术博客" type="application/atom+xml">
  <script src="/javascripts/modernizr-2.0.js"></script>
  <script src="http://cdn.staticfile.org/jquery/1.9.1/jquery.min.js"></script>
  <script>!window.jQuery && document.write(unescape('%3Cscript src="/javascripts/libs/jquery.min.js"%3E%3C/script%3E'))</script>
  <script src="/javascripts/octopress.js" type="text/javascript"></script>
  

</head>

<body   >
  <header role="banner"><hgroup>
  <h1><a href="/">许龙武的技术博客</a></h1>
  
    <h2>记录所学，快乐分享</h2>
  
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="/atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<form action="https://www.google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="sitesearch" value="loongwoo.github.io">
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form>
  
<ul class="main-navigation">
	<li><a href="/">首页</a></li>
	<li><a href="/blog/archives">所有文章</a></li>
	<li><a href="http://weibo.com/loongwoo">我的微博</a></li>
</ul>

</nav>
  <div id="main">
    <div id="content">
      <div>
<article class="hentry" role="article">
  
  <header>
    
      <h1 class="entry-title">IOS NSString常用Category</h1>
    
    
      <p class="meta">
        




<time class='entry-date' datetime='2015-07-08T17:34:44+08:00'><span class='date'><span class='date-month'>Jul</span> <span class='date-day'>8</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>5:34 pm</span></time>
        
      </p>
    
  </header>


<div class="entry-content"><p>我们经常会在项目中用一些NSString的Category，可以记录下来，免得每次都要查。比如NSString的URL编码、MD5加密、自适应大小等等。</p>

<h2>方法解析</h2>

<h3>汉字转拼音</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nf">transformToPinyin</span> <span class="p">{</span>
</span><span class='line'>    <span class="bp">NSMutableString</span> <span class="o">*</span><span class="n">mutableString</span> <span class="o">=</span> <span class="p">[</span><span class="bp">NSMutableString</span> <span class="nl">stringWithString</span><span class="p">:</span><span class="nb">self</span><span class="p">];</span>
</span><span class='line'>    <span class="n">CFStringTransform</span><span class="p">((</span><span class="n">CFMutableStringRef</span><span class="p">)</span><span class="n">mutableString</span><span class="p">,</span> <span class="nb">NULL</span><span class="p">,</span> <span class="n">kCFStringTransformToLatin</span><span class="p">,</span> <span class="nb">false</span><span class="p">);</span>
</span><span class='line'>    <span class="n">mutableString</span> <span class="o">=</span> <span class="p">(</span><span class="bp">NSMutableString</span> <span class="o">*</span><span class="p">)[</span><span class="n">mutableString</span> <span class="nl">stringByFoldingWithOptions</span><span class="p">:</span><span class="n">NSDiacriticInsensitiveSearch</span> <span class="nl">locale</span><span class="p">:[</span><span class="bp">NSLocale</span> <span class="n">currentLocale</span><span class="p">]];</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">mutableString</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>尽管对汉字的排序可以用<code>- (NSComparisonResult)localizedCompare:(NSString *)string;</code>进行排序，但也有时候需要手动排序，这个拓展就有用了。</p>

<h3>URL编码</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nf">URLEncodedString</span><span class="p">{</span>
</span><span class='line'>    <span class="bp">NSString</span> <span class="o">*</span><span class="n">encodedString</span> <span class="o">=</span> <span class="p">(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="n">CFBridgingRelease</span><span class="p">(</span><span class="n">CFURLCreateStringByAddingPercentEscapes</span><span class="p">(</span><span class="n">kCFAllocatorDefault</span><span class="p">,</span>
</span><span class='line'>        <span class="p">(</span><span class="n">CFStringRef</span><span class="p">)</span><span class="nb">self</span><span class="p">,</span>
</span><span class='line'>        <span class="p">(</span><span class="n">CFStringRef</span><span class="p">)</span><span class="s">@&quot;!$&amp;&#39;()*+,-./:;=?@_~%#[]&quot;</span><span class="p">,</span>
</span><span class='line'>        <span class="nb">NULL</span><span class="p">,</span>
</span><span class='line'>        <span class="n">kCFStringEncodingUTF8</span><span class="p">));</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">encodedString</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>对于包含中文的URL不能整体转化为UTF-8的格式，那么就可以用<code>CFURLCreateStringByAddingPercentEscapes</code>来排除掉一些应该保留的字符。</p>

<h3>Json字符串转化为Object</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="kt">id</span><span class="p">)</span><span class="nf">JSONObject</span><span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="bp">NSJSONSerialization</span> <span class="nl">JSONObjectWithData</span><span class="p">:[</span><span class="nb">self</span> <span class="nl">dataUsingEncoding</span><span class="p">:</span><span class="n">NSUTF8StringEncoding</span><span class="p">]</span> <span class="nl">options</span><span class="p">:</span><span class="n">kNilOptions</span> <span class="nl">error</span><span class="p">:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>网络接口很多是Json的字符串，该方法直接转化为NSArray、NSDictionary等。</p>

<h3>MD5加密</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="bp">NSString</span><span class="o">*</span><span class="p">)</span><span class="nf">md5</span><span class="p">{</span>
</span><span class='line'>    <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span><span class="n">cStr</span> <span class="o">=</span> <span class="p">[</span><span class="nb">self</span> <span class="n">UTF8String</span><span class="p">];</span>
</span><span class='line'>    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">result</span><span class="p">[</span><span class="n">CC_MD5_DIGEST_LENGTH</span><span class="p">];</span>
</span><span class='line'>    <span class="n">CC_MD5</span><span class="p">(</span> <span class="n">cStr</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span><span class="n">strlen</span><span class="p">(</span><span class="n">cStr</span><span class="p">),</span> <span class="n">result</span> <span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="bp">NSString</span> <span class="nl">stringWithFormat</span><span class="p">:</span> <span class="s">@&quot;%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X&quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">6</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">7</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">8</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">9</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">10</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">11</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">12</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">13</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">14</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">15</span><span class="p">]];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>MD5加密也是常用的方法，记得导入<code>#import &lt;CommonCrypto/CommonDigest.h&gt;</code>。</p>

<h3>UTF16的MD5加密</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nf">md5ForUTF16</span><span class="p">{</span>
</span><span class='line'>    <span class="bp">NSData</span> <span class="o">*</span><span class="n">temp</span> <span class="o">=</span> <span class="p">[</span><span class="nb">self</span> <span class="nl">dataUsingEncoding</span><span class="p">:</span><span class="n">NSUTF16LittleEndianStringEncoding</span><span class="p">];</span>
</span><span class='line'>    <span class="kt">UInt8</span> <span class="o">*</span><span class="n">cStr</span> <span class="o">=</span> <span class="p">(</span><span class="kt">UInt8</span> <span class="o">*</span><span class="p">)[</span><span class="n">temp</span> <span class="n">bytes</span><span class="p">];</span>
</span><span class='line'>    <span class="kt">unsigned</span> <span class="kt">char</span> <span class="n">result</span><span class="p">[</span><span class="n">CC_MD5_DIGEST_LENGTH</span><span class="p">];</span>
</span><span class='line'>    <span class="n">CC_MD5</span><span class="p">(</span><span class="n">cStr</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)[</span><span class="n">temp</span> <span class="n">length</span><span class="p">],</span> <span class="n">result</span><span class="p">);</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="bp">NSString</span> <span class="nl">stringWithFormat</span><span class="p">:</span><span class="s">@&quot;%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x&quot;</span><span class="p">,</span> <span class="n">result</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">6</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">7</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">8</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">9</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">10</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">11</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">12</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">13</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">14</span><span class="p">],</span> <span class="n">result</span><span class="p">[</span><span class="mi">15</span><span class="p">]];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>这个加密倒不是很常用，经常有前面一个就够用了，这里统一记录一下。</p>

<h3>去掉前后的空格</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nf">trim</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="nb">self</span> <span class="nl">stringByTrimmingCharactersInSet</span><span class="p">:[</span><span class="bp">NSCharacterSet</span> <span class="n">whitespaceAndNewlineCharacterSet</span><span class="p">]];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>记得在写C#的时候，经常要带着trim函数走。但是IOS里面用的地方不多，记得用过的就是URL的参数，以及在要把输入内容转化为数字的时候。在输入结束的时候trim一下是个应该坚持的习惯。</p>

<h3>去掉行中多余空格</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="bp">NSString</span> <span class="o">*</span><span class="p">)</span><span class="nf">trimTheExtraSpaces</span><span class="p">{</span>
</span><span class='line'>    <span class="bp">NSCharacterSet</span> <span class="o">*</span><span class="n">whitespaces</span> <span class="o">=</span> <span class="p">[</span><span class="bp">NSCharacterSet</span> <span class="n">whitespaceCharacterSet</span><span class="p">];</span>
</span><span class='line'>    <span class="bp">NSPredicate</span> <span class="o">*</span><span class="n">noEmptyStrings</span> <span class="o">=</span> <span class="p">[</span><span class="bp">NSPredicate</span> <span class="nl">predicateWithFormat</span><span class="p">:</span><span class="s">@&quot;SELF != &#39;&#39;&quot;</span><span class="p">];</span>
</span><span class='line'>    <span class="bp">NSArray</span> <span class="o">*</span><span class="n">parts</span> <span class="o">=</span> <span class="p">[</span><span class="nb">self</span> <span class="nl">componentsSeparatedByCharactersInSet</span><span class="p">:</span><span class="n">whitespaces</span><span class="p">];</span>
</span><span class='line'>    <span class="bp">NSArray</span> <span class="o">*</span><span class="n">filteredArray</span> <span class="o">=</span> <span class="p">[</span><span class="n">parts</span> <span class="nl">filteredArrayUsingPredicate</span><span class="p">:</span><span class="n">noEmptyStrings</span><span class="p">];</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="n">filteredArray</span> <span class="nl">componentsJoinedByString</span><span class="p">:</span><span class="s">@&quot; &quot;</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>该方法会去掉string中的多余空格，有多个的话留一个。</p>

<h3>是否是空字符</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">isEmpty</span> <span class="p">{</span>
</span><span class='line'>    <span class="bp">NSCharacterSet</span> <span class="o">*</span><span class="n">charSet</span> <span class="o">=</span> <span class="p">[</span><span class="bp">NSCharacterSet</span> <span class="n">whitespaceAndNewlineCharacterSet</span><span class="p">];</span>
</span><span class='line'>    <span class="bp">NSString</span> <span class="o">*</span><span class="n">trimmed</span> <span class="o">=</span> <span class="p">[</span><span class="nb">self</span> <span class="nl">stringByTrimmingCharactersInSet</span><span class="p">:</span><span class="n">charSet</span><span class="p">];</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="n">trimmed</span> <span class="nl">isEqualToString</span><span class="p">:</span><span class="s">@&quot;&quot;</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>我们常常用<code>if( nil == tempStr || 0 == tempStr.lenth ){...}</code>来判断是否为空。但比如<code>NSString \*tmp4=@"   ";</code>这样一个字符串，<code>[tmp4 length]</code>求的长度为3。但是用上面的方法可以看到是空的。</p>

<h3>自适应大小</h3>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="p">-(</span><span class="bp">CGSize</span><span class="p">)</span><span class="nf">sizeWithFont:</span><span class="p">(</span><span class="bp">UIFont</span> <span class="o">*</span><span class="p">)</span><span class="nv">font</span>
</span><span class='line'>    <span class="nf">constrainedToSize:</span><span class="p">(</span><span class="bp">CGSize</span><span class="p">)</span><span class="nv">constrainedSize</span>
</span><span class='line'>            <span class="nf">lineBreakMode:</span><span class="p">(</span><span class="n">NSLineBreakMode</span><span class="p">)</span><span class="nv">lineBreakMode</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">([</span><span class="nb">self</span> <span class="nl">respondsToSelector</span><span class="p">:</span><span class="k">@selector</span><span class="p">(</span><span class="nl">boundingRectWithSize</span><span class="p">:</span><span class="nl">options</span><span class="p">:</span><span class="nl">attributes</span><span class="p">:</span><span class="nl">context</span><span class="p">:)])</span> <span class="p">{</span>
</span><span class='line'>        <span class="bp">NSMutableParagraphStyle</span> <span class="o">*</span><span class="n">paragraphStyle</span> <span class="o">=</span> <span class="p">[[</span><span class="bp">NSMutableParagraphStyle</span> <span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span>
</span><span class='line'>        <span class="n">paragraphStyle</span><span class="p">.</span><span class="n">lineBreakMode</span> <span class="o">=</span> <span class="n">lineBreakMode</span><span class="p">;</span>
</span><span class='line'>        <span class="bp">NSDictionary</span> <span class="o">*</span><span class="n">attributes</span> <span class="o">=</span> <span class="l">@{</span><span class="nl">NSFontAttributeName</span><span class="p">:</span><span class="n">font</span><span class="p">,</span> <span class="nl">NSParagraphStyleAttributeName</span><span class="p">:</span><span class="n">paragraphStyle</span><span class="l">}</span><span class="p">;</span>
</span><span class='line'>        <span class="bp">CGRect</span> <span class="n">boundingRect</span> <span class="o">=</span> <span class="p">[</span><span class="nb">self</span> <span class="nl">boundingRectWithSize</span><span class="p">:</span><span class="n">constrainedSize</span> <span class="nl">options</span><span class="p">:</span><span class="n">NSStringDrawingUsesLineFragmentOrigin</span>  <span class="nl">attributes</span><span class="p">:</span><span class="n">attributes</span> <span class="nl">context</span><span class="p">:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">CGSizeMake</span><span class="p">(</span><span class="n">ceilf</span><span class="p">(</span><span class="n">boundingRect</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">width</span><span class="p">),</span> <span class="n">ceilf</span><span class="p">(</span><span class="n">boundingRect</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">height</span><span class="p">));</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'><span class="cp">#pragma clang diagnostic push</span>
</span><span class='line'><span class="cp">#pragma clang diagnostic ignored &quot;-Wdeprecated-declarations&quot;</span>
</span><span class='line'>    <span class="k">return</span> <span class="p">[</span><span class="nb">self</span> <span class="nl">sizeWithFont</span><span class="p">:</span><span class="n">font</span> <span class="nl">constrainedToSize</span><span class="p">:</span><span class="n">constrainedSize</span> <span class="nl">lineBreakMode</span><span class="p">:</span><span class="n">lineBreakMode</span><span class="p">];</span>
</span><span class='line'><span class="cp">#pragma clang diagnostic pop</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>这个就再熟悉不过了，不过定义Category的时候要记得导入<code>#import &lt;UIKit/UIKit.h&gt;</code>，否则CGSize这些会报错。还有一个用法</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='objectivec'><span class='line'><span class="cp">#pragma clang diagnostic push</span>
</span><span class='line'><span class="cp">#pragma clang diagnostic ignored &quot;-相关命令&quot;</span>
</span><span class='line'><span class="c1">// 你自己的代码</span>
</span><span class='line'><span class="cp">#pragma clang diagnostic pop</span>
</span></code></pre></td></tr></table></div></figure>


<p>#pragma我们常用来做分段注释，还有一个功能是处理编译器警告。<code>-Wdeprecated-declarations</code>表示方法弃用。加了这个之后，就不会再出现黄色警告了。<br/>
其他的情况可以查询<a href="http://fuckingclangwarnings.com">Which Clang Warning Is Generating This Message?</a></p>

<h2>源码下载</h2>

<p><a href="https://github.com/Loongwoo/StringExtensionDemo">StringExtensionDemo</a></p>

<p>在项目中导入NSString+Extensions.h和NSString+Extensions.m即可。</p>
</div>


  <footer>
    <p class="meta">
      
  

<span class="byline author vcard">Posted by <span class="fn">许龙武</span></span>

      




<time class='entry-date' datetime='2015-07-08T17:34:44+08:00'><span class='date'><span class='date-month'>Jul</span> <span class='date-day'>8</span><span class='date-suffix'>th</span>, <span class='date-year'>2015</span></span> <span class='time'>5:34 pm</span></time>
      


      <br>原文链接：<a href="http://loongwoo.github.io/blog/2015/07/08/at-ios-nsstringchang-yong-category/">http://loongwoo.github.io/blog/2015/07/08/at-ios-nsstringchang-yong-category/</a>
 <!-- 添加这一行代码 -->
    </p>
    
      <div class="sharing">
  
  
  
  
  <!-- JiaThis Button BEGIN -->
<div class="jiathis_style_32x32">
  <a class="jiathis_button_qzone"></a>
  <a class="jiathis_button_tsina"></a>
  <a class="jiathis_button_tqq"></a>
  <a class="jiathis_button_weixin"></a>
  <a class="jiathis_button_renren"></a>
  <a href="http://www.jiathis.com/share?uid=2043882" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"></a>
  <a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript">var jiathis_config = {data_track_clickback:'true'};</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=2043882" charset="utf-8"></script>
<!-- JiaThis Button END -->

<!--UY BEGIN -->
<div id="uyan_frame"></div>
<script type="text/javascript" src="http://v2.uyan.cc/code/uyan.js?uid=2043882"></script>
<!--UY END -->

  
</div>

    
    <p class="meta">
      
        <a class="basic-alignment left" href="/blog/2015/07/07/at-ios-pdfxian-shi-he-liu-lan/" title="Previous Post: IOS PDF显示和浏览">&laquo; IOS PDF显示和浏览</a>
      
      
        <a class="basic-alignment right" href="/blog/2015/07/09/at-iosxiao-xi-zhuan-fa-ji-zhi/" title="Next Post: IOS消息转发机制">IOS消息转发机制 &raquo;</a>
      
    </p>
  </footer>
</article>

</div>

<aside class="sidebar">
  
    <section>
  <h1>最新文章</h1>
  <ul id="recent_posts">
    
      <li class="post">
        <a href="/blog/2016/01/28/stringmanage-a-xcode-plugin-for-managing-your-strings/">StringManage -- a Xcode Plugin for Managing Your Strings</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/12/iosshi-yong-bundle/">iOS使用Bundle</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/12/iosduo-ge-localizable-dot-strings/">IOS多个Localizable.strings</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/11/iosxian-zhi-uitextfieldchang-du/">IOS限制UITextField长度</a>
      </li>
    
      <li class="post">
        <a href="/blog/2015/09/11/ioszi-dong-bu-ju-constraintswithvisualformat/">IOS自动布局constraintsWithVisualFormat</a>
      </li>
    
  </ul>
</section>




<section>
	<h1>关于我</h1>
    <p>程序员、创业者、IOS开发工程师
	<br/>新浪微博：<a href='http://weibo.com/loongwoo' target='_blank'>商陆_Xu</a>
	<br/>新浪博客：<a href="http://blog.sina.com.cn/lunaize" target="_blank" title="商陆的博客">商陆</a>
	<br/>GitHub：<a href='https://github.com/loongwoo' target='_blank'>Loongwoo</a>
	<br/>Git@OSC: <a href='http://git.oschina.net/redder_xu' target='_blank'>redder_xu</a>
    <br/>CSDN博客：<a href="http://blog.csdn.net/redder_xu" target="_blank" title="CSDN">霜叶红于二月花</a>
	<br/>个人微信：xulongwu77 欢迎调戏
	<br/><img width="220px" src="/images/myweixin.jpg" />
	</p>
</section>
<section>
  <h1>友情链接</h1>
  <ul>
  <li><a href="http://blog.devtang.com" target="_blank" title="唐巧的技术博客">唐巧的技术博客</a></li>
  <li><a href="http://www.leichunfeng.com" target="_blank" title="雷纯锋的技术博客">雷纯锋的技术博客</a></li>
  <li><a href="http://www.superqq.com" target="_blank" title=“刚刚在线”>刚刚在线</a></li>
  </ul>
</section>

  
</aside>


    </div>
  </div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2017 - 许龙武 -
  <span class="credit">Powered by <a href="http://octopress.org">Octopress</a></span>
  <script language="javascript" type="text/javascript" src="http://js.users.51.la/17858326.js"></script>
<noscript><a href="http://www.51.la/?17858326" target="_blank"><img alt="&#x6211;&#x8981;&#x5566;&#x514D;&#x8D39;&#x7EDF;&#x8BA1;" src="http://img.users.51.la/17858326.asp" style="border:none" /></a></noscript>

</p>

</footer>
  











</body>
</html>
